package com.modular.iot.bo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateUtil;
import com.ToolOpt;
import com.modular.iot.bean.ThingsAttributeDo;
import lombok.Data;
import lombok.NoArgsConstructor;
import ntd.util.config.common.bo.ComDictVo;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * iot 配置详情
 */
@Data
@NoArgsConstructor
public class IotAttribute extends ThingsAttributeDo {

    private List<ComDictVo> dict = new ArrayList<>();

    public static IotAttribute init(String code, String name, String val) {
        return init(code, name, val, null, DateUtil.date());
    }

    public static IotAttribute init(String code, String name, String val, String unit) {
        return init(code, name, val, unit, DateUtil.date());
    }

    public static IotAttribute init(String code, String name, String val, String unit, Date dateTime) {
        IotAttribute iotAttribute = new IotAttribute();
        iotAttribute.setCode(code);
        iotAttribute.setName(name);
        iotAttribute.setVal(val);
        iotAttribute.setUnit(unit);
        iotAttribute.setUpdatedDate(dateTime);
        return iotAttribute;
    }

    public static IotAttribute init(String code, String name, String val, String unit, LocalDateTime dateTime) {
        String localDateTime = DateUtil.formatLocalDateTime(dateTime);
        return init(code, name, val, unit, DateUtil.parseDate(localDateTime));
    }

    public static IotAttribute transform(ThingsAttributeDo thingsAttributeDo) {
        // iot_attribute_type - blowdown_valve
        IotAttribute iotAttribute = BeanUtil.copyProperties(thingsAttributeDo, IotAttribute.class);
        iotAttribute.setDict(CollStreamUtil.toList(ToolOpt.OPT_S_DICT.listByCode(iotAttribute.getCode()), ComDictVo::new));
        return iotAttribute;
    }
}
